#include <QDebug>
#include <QWebFrame>
#include <QMainWindow>
#include <QString>
#include <QDateTime>
#include <QMessageBox>
#include "Sensors.h"
#include "SymbianSDKSensors.h"

Sensors::Sensors()
{
	qDebug() << "Sensors object created";
	m_symbianSDKSensors = new SymbianSDKSensors(*this);
}

Sensors::~Sensors()
{
	delete m_symbianSDKSensors;
}

void Sensors::setEnvironment(QMainWindow *parentWindow, QWebFrame *webFrame)
{
	m_parentWindow = parentWindow;
	m_webFrame = webFrame;

	webFrame->addToJavaScriptWindowObject("sensor", this);
}

void Sensors::findSensorChannel(const QString &criteria)
{
	m_symbianSDKSensors->findSensorChannel(criteria);
}
void Sensors::addChannelInfo(int ChannelId, int ContextType, int Quantity, int ChannelType, const QString &Location, const QString &VendorId, int DataItemSize, int ChannelDataTypeId)
{
	QString javaScriptString = QString("addChannelInfo(%1, %2, %3, %4, \"%5\", \"%6\", %7, %8)").arg(ChannelId).arg(ContextType).arg(Quantity).arg(ChannelType).arg(Location).arg(VendorId).arg(DataItemSize).arg(ChannelDataTypeId);
	m_webFrame->evaluateJavaScript(javaScriptString);
}

void Sensors::accelerometerEvent(int transactionId, int axisX, int axisY, int axisZ, long timeStamp)
{
	// 2 - Event completed
	// 4 - Event error
	// 9 - Event in progress
	QString javaScriptString = QString("accelerometerAxisDataEvent(%1, 2, \"AxisData\", \"%2\", %3, %4, %5)").arg(transactionId).arg(QDateTime::currentDateTime().toString()).arg(axisX).arg(axisY).arg(axisZ);
	m_webFrame->evaluateJavaScript(javaScriptString);
}

void Sensors::orientationEvent(int transactionId, int deviceOrientation, long timeStamp)
{
	QString devOrient("");
	switch(deviceOrientation)
	{
	case 0:
		devOrient = "Undefined";
		break;
	case 1:
		devOrient = "DisplayUp";
		break;
	case 2:
		devOrient = "DisplayDown";
		break;
	case 3:
		devOrient = "DisplayLeftUp";
		break;
	case 4:
		devOrient = "DisplayRightUp";
		break;
	case 5:
		devOrient = "DisplayUpwards";
		break;
	case 6:
		devOrient = "DisplayDownwards";
		break;
	}

	QString javaScriptString = QString("orientationDataEvent(%1, 2, \"OrientationData\", \"%2\", \"%3\")").arg(transactionId).arg(QDateTime::currentDateTime().toString()).arg(devOrient);
	m_webFrame->evaluateJavaScript(javaScriptString);
}

void Sensors::tappingEvent(int transactionId, int direction, long timeStamp)
{
	QString javaScriptString = QString("accelerometerDoubleTappingDataEvent(%1, 2, \"DoubleTappingData\", \"%2\", %3)").arg(transactionId).arg(QDateTime::currentDateTime().toString()).arg(direction);
	m_webFrame->evaluateJavaScript(javaScriptString);
}

int Sensors::registerForNotification(const QString &listeningType, int ChannelId, int ContextType, int Quantity, int ChannelType, const QString &Location, const QString &VendorId, int DataItemSize, int ChannelDataTypeId)
{
	// TODO check here listening type
	int retval = -1;
	retval = m_symbianSDKSensors->registerForNotification(ChannelId, ContextType, Quantity, ChannelType, Location, VendorId, DataItemSize, ChannelDataTypeId);
	return retval;
}


void Sensors::cancel(int criteria)
{
	m_symbianSDKSensors->cancel(criteria);
}


void Sensors::getChannelProperty(const QString &listeningType, int ChannelId, int ContectType, int Quantity, int ChannelType, const QString &location, const QString &VendorId, int DataItemSize, int ChannelDataTypeId, const QString &PropertyId)
{
}
